//下载依赖
import ts from "rollup-plugin-typescript2" // 解析js
import json from "@rollup/plugin-json" //解析json
import resolvePlugin from "@rollup/plugin-node-resolve" //解析第三方插件
import babel from "babel-plugin-transform-remove-strict-mode"
import path from 'path'
import { fileURLToPath } from "node:url";
import { dirname } from "node:path"
import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

let packagesDir = path.resolve(__dirname,"packages");

let packageDir = path.resolve(packagesDir,process.env.TARGET);

let resolve = p=>{
  return path.resolve(packageDir,p);
}


const pkg = require(resolve(`package.json`))
const packageOptions = pkg.buildOptions || {}
const name = path.basename(packageDir)

const outputOptions = {
  "esm-bundler":{
    file:resolve(`dist/${name}.esm-bundler.js`),
    format:"es",
  },
  "cjs":{
    file:resolve(`dist/${name}.cjs.js`),
    format:"cjs",
  },
  "global":{
    file:resolve(`dist/${name}.global.js`),
    format:"iife",
  }
}
function createConfig(format,output){
  output.name = packageOptions.name;
  output.sourcemap = true;
  //生成rollup配置
  return {
    input:resolve("src/index.ts"),
    output,
    plugins:[
      json(),
      ts({
        tsconfig:path.resolve(__dirname,"tsconfig.json")
      }),
      resolvePlugin(),
      babel()
    ]
  }
}
export default packageOptions.formats.map(
  (format)=>{
    return createConfig(format,outputOptions[format])
  }
  
) 